package com.ponhu.ea2.goodsCenter.provider.facade.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ponhu.ea2.constants.CommonConstant;
import com.ponhu.ea2.dto.OperationRecordContentDto;
import com.ponhu.ea2.dto.PhInventoryDetailDto;
import com.ponhu.ea2.entity.Inventory;
import com.ponhu.ea2.entity.OperationRecordLogs;
import com.ponhu.ea2.enums.YesOrNoEnum;
import com.ponhu.ea2.goodsCenter.provider.mapper.InventoryMapper;
import com.ponhu.ea2.goodsCenter.provider.mapper.OperationRecordLogsMapper;
import com.ponhu.ea2.goodsCenter.provider.mapper.PhInventoryDetailMapper;
import com.ponhu.ea2.goodsCenter.provider.service.InventoryService;
import com.ponhu.ea2.service.facade.PhInventoryDetailApiService;
import com.ponhu.ea2.vo.PhEditPriceLogExcelVo;
import com.ponhu.ea2.vo.PhInventoryDetailExcelVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author luyu
 * @create 2021-12-15 14:47
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = PhInventoryDetailApiService.class, timeout = 30000)
public class PhInventoryDetailApiServiceImpl implements PhInventoryDetailApiService {

    @Autowired
    PhInventoryDetailMapper phInventoryDetailMapper;

    @Autowired
    OperationRecordLogsMapper operationRecordLogsMapper;

    @Autowired
    InventoryMapper inventoryMapper;

    @Resource
    InventoryService inventoryService;

    @Override
    public List<PhInventoryDetailExcelVo> queryAll(PhInventoryDetailDto phInventoryDetailDto) {

        return phInventoryDetailMapper.getAllPhInventoryDetail(phInventoryDetailDto).stream().map(item -> {
            if (YesOrNoEnum.YES.getCode().toString().equals(item.getIsSecondhand())) {
                item.setIsSecondhand(YesOrNoEnum.YES.getName());
            }
            if (YesOrNoEnum.NO.getCode().toString().equals(item.getIsSecondhand())) {
                item.setIsSecondhand(YesOrNoEnum.NO.getName());
            }
            return item;
        }).collect(Collectors.toList());
    }

    @Override
    public List<PhEditPriceLogExcelVo> queryEditPriceLog(PhInventoryDetailDto phInventoryDetailDto) {
        log.info("导出价格变动日志表开始,入参{}", JSON.toJSONString(phInventoryDetailDto));
        long start = System.currentTimeMillis();
        List<OperationRecordLogs> operationRecordLogs = operationRecordLogsMapper.selectList(new LambdaQueryWrapper<OperationRecordLogs>()
                .between(OperationRecordLogs::getCreateTime,
                        phInventoryDetailDto.getStartTime(),
                        phInventoryDetailDto.getEndTime())
                .eq(OperationRecordLogs::getBusinessTable, CommonConstant.BUSINESS_TYPE_INVENTORY)
                .eq(OperationRecordLogs::getLogType, CommonConstant.OPERATION_RECORD_LOG_TYPE.TABLE_COLUMNS_CHANGE));
        Map<Long, String> idAndCodeMap = inventoryService.listByIds(
                operationRecordLogs.stream()
                        .map(OperationRecordLogs::getRecordId)
                        .collect(Collectors.toList()))
                .stream()
                .collect(Collectors.toMap(Inventory::getId, Inventory::getInventoryCode));

        // 转化为PhEditPriceLogExcelVo
        List<PhEditPriceLogExcelVo> collect = operationRecordLogs.stream()
                .filter(item -> item.getContent().contains("含税销售价")
                        || item.getContent().contains("含税供货价")
                        || item.getContent().contains("入库价"))
                .map(item -> {
                    String inventoryCode = idAndCodeMap.get(item.getRecordId());
                    if (StringUtils.isNotEmpty(inventoryCode)) {
                        PhEditPriceLogExcelVo phEditPriceLogExcelVo = new PhEditPriceLogExcelVo()
                                .setEditName(item.getOperator())
                                .setEditTime(item.getCreateTime());
                        phEditPriceLogExcelVo.setInventoryCode(inventoryCode);
                        List<OperationRecordContentDto> operationRecordContentDtos =
                                JSONArray.parseArray(item.getContent(), OperationRecordContentDto.class);
                        operationRecordContentDtos.stream().forEach(json -> {
                            if (json.getColumnDesc().contains("含税销售价")) {
                                phEditPriceLogExcelVo.setOldSaleTaxPrice(json.getOldValue());
                                phEditPriceLogExcelVo.setNewSaleTaxPrice(json.getNewValue());
                            }
                            if (json.getColumnDesc().contains("含税供货价")) {
                                phEditPriceLogExcelVo.setOldSupplyTaxPrice(json.getOldValue());
                                phEditPriceLogExcelVo.setNewSupplyTaxPrice(json.getNewValue());
                            }
                            if (json.getColumnDesc().contains("入库价")) {
                                phEditPriceLogExcelVo.setOldWarehousePrice(json.getOldValue());
                                phEditPriceLogExcelVo.setNewWarehousePrice(json.getNewValue());
                            }
                        });
                        return phEditPriceLogExcelVo;
                    }
                    return null;
                }).collect(Collectors.toList());
        log.info("价格变动日志表,总共耗时{}",(System.currentTimeMillis()-start));
        return collect;
    }
}
